//
//  ARES.m
//  OR_1
//
//  Created by Luboš Kulič on 6/16/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
// 28411391

#import "ARES.h"


@implementation ARES

-(id)init {
	baseURL = @"http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_std.cgi?";
	ORbaseURL = @"http://wwwinfo.mfcr.cz/cgi-bin/ares/darv_or.cgi?";
	net = [[Network alloc] init];
		
	return self;
}


// ----------

-(NSString *) buildQueryForName:(NSString *)companyName andID:(NSString *)companyID {
	NSMutableString * res = [NSMutableString string];
	
	
	BOOL first = true;
	
	if(companyName) {
 		[res appendString:@"obchodni_firma="];
		[res appendString:companyName];
		first = false;
	}
	
	if(companyID) {
		if(!first) {
			[res appendString:@"&"];
		} else {
			first = false;
		}
		[res appendString:@"ico="];
		[res appendString:companyID];
	}
	
	return res;
	
}


-(void) searchForCompany:(NSString *)companyName orID:(NSString *) companyID notifyEnd:(id<ARESEventListener>) listener_{
	listener = listener_;
	
	NSString * request = [baseURL stringByAppendingString: 
						  [self buildQueryForName:companyName andID:companyID]];
	
	NSLog(request);
	
	[net getData:request notifyEnd:self];
}


//  28411391

-(void) searchInORfor:(NSString *)companyID notifyEnd:(id<ARESEventListener>) listener_ {
	listener = listener_;
	
	NSString * request = [ORbaseURL stringByAppendingString:
						  [self buildQueryForName:0 andID:companyID]]; // we don't need to retain after the method ends
	
	NSLog(request);
	
	[net getData:request notifyEnd:self];	
	
}


-(void) network:(Network *)net didFinishLoadingWithResult:(NSData *)data {
	
	[listener ARES:self didFinishSearchWithResult:data];
}

@end
